package com.smrs.deliver.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.util.CollectionUtils;

import com.jof.framework.dao.hibernate.BaseDAOHibernateImpl;
import com.smrs.deliver.model.DeliverSequenceModel;


public class DeliverSequenceDao extends BaseDAOHibernateImpl<DeliverSequenceModel, Long>{
	
	public String getDeliverSequenceValue(){
		String hsql = " from DeliverSequenceModel";
		//Object values = new Object[]{};
		Query query = this.createHQLQuery(hsql, new Object[]{});
		
		List list = query.list();
		DeliverSequenceModel model = null;
		if(CollectionUtils.isEmpty(list)){
			model = new DeliverSequenceModel();
			model.setDeliverDetailSeq(1l);
			model.setDeliverSequence(1l);
			save(model);		
		}else{
			model = (DeliverSequenceModel)list.get(0);
		}
		model.setDeliverSequence(model.getDeliverSequence()+1l);
		update(model);
		Long sequenceValue = model.getDeliverSequence();
		String tempSeq = "ASN0000000"+sequenceValue;
		tempSeq.substring(tempSeq.length()-10);
		return tempSeq;
	}
	
	public Long getDeliverDetailSeqNextValue(){
		String hsql = " from DeliverSequenceModel";
		//Object values = new Object[]{};
		Query query = this.createHQLQuery(hsql, new Object[]{});
		
		List list = query.list();
		DeliverSequenceModel model = null;
		if(CollectionUtils.isEmpty(list)){
			model = new DeliverSequenceModel();
			model.setDeliverDetailSeq(1l);
			model.setDeliverSequence(1l);
			save(model);		
		}else{
			model = (DeliverSequenceModel)list.get(0);
		}
		//model.setDeliverSequence(model.getDeliverSequence()+1l);
		model.setDeliverDetailSeq(model.getDeliverDetailSeq()+1);
		update(model);
		Long sequenceValue = model.getDeliverDetailSeq();
		return sequenceValue;
	}
	
	
}
